import {defineStore} from 'pinia';
import {http} from "src/utils/http";
import CryptoJS from "crypto-js"
import {conf} from "src/utils/serverHost"
import {useQuasar} from "quasar";
import axios from "axios";

let $q = useQuasar()
let time = null;
export const useQaStore = defineStore('qaStream', {
    state: () => ({
        allQaMsg: [],
        uuid: '',
        callIng: false,
        extendType: [{
            type: 'EventGuide',
            name: '查看事项指南'
        }, {
            type: 'EventSituation',
            name: '查看事项情形'
        }, {
            type: 'MaterialList',
            name: '查看办件材料'
        }]
    }),
    getters: {
        // 这里是你的 getters
    },
    actions: {
        sseClient() {
            const uuid = CryptoJS.lib.WordArray.random(16); // 生成 128 位的随机字节序列
            this.uuid = CryptoJS.enc.Hex.stringify(uuid).toUpperCase();// 将字节序列转换为十六进制表示形式，并转换为大写字母
            let es = new EventSource(`${conf.sseHost}/connect?uuid=${this.uuid}`, {withCredentials: true});
            setTimeout(() => {
                this.sendPant()
            }, 10000)
            es.onerror = function (event) {
                es.close();
            }

            es.onmessage = (event) => {
                try {
                    let node = this.allQaMsg[this.allQaMsg.length - 1];
                    // 获取模型回答中携带的事项信息
                    if (JSON.parse(event.data).KG) node.KG = JSON.parse(event.data).KG
                    //联想出来的事项数组
                    else if (JSON.parse(event.data).Events) node.Events = JSON.parse(event.data).Events
                    //如果存在关联的推荐问题
                    else if (JSON.parse(event.data).Guide) node.Guide = JSON.parse(event.data).Guide
                    // 获取模型回答的文本内容
                    else node.answer = JSON.parse(event.data);

                    console.log(JSON.parse(event.data))

                } catch (e) {

                    this.allQaMsg[this.allQaMsg.length - 1].answer = '返回数据格式错误，不是一个JSON数据';
                    this.callIng = false
                    this.allQaMsg[this.allQaMsg.length - 1] = false;

                }
            }
        },
        sendPant() {
            axios.post(`${conf.sseHost}/query`, {
                uuid: this.uuid,
            }).then(() => {
                clearTimeout(time)
                time = setTimeout(() => {
                    this.sendPant();
                }, 3000)
            }).catch(err => {
                //todo
            })
        },
        // 发送消息方法
        sendQuestion(message) {
            let para = {
                uuid: this.uuid,
                event: '',
                msg: message,
                areaCode: '510603000000'
            }
            this.allQaMsg.push({
                uuid: this.uuid,
                question: message,
                extendType: null,
                answer: '',
                callIng: true
            })
            this.callIng = true;
            //通过以下http请求表示已像服务端提出问题，等待从EventSource返回答案
            axios.post(`${conf.sseHost}/query`, para).then(() => {
                //todo
            }).catch(err => {
                //todo
            })
        },
        sendOcr(base64) {
            let para = {
                uuid: this.uuid,
                img: base64
            }
            this.allQaMsg.push({
                uuid: this.uuid,
                question: 'OCR识别',
                extendType: null,
                answer: '',
                callIng: true
            })
            this.callIng = true;
            //通过以下http请求表示已像服务端提出问题，等待从EventSource返回答案
            axios.post(`${conf.sseHost}/query`, para).then(() => {
                //todo
            }).catch(err => {
                this.callIng = false;
            })
        },
        // 添加一个查询图数据库的方法
        sendSearch(x) {
            if (this.callIng) {
                return
            }
            // const uuid = CryptoJS.lib.WordArray.random(16); // 生成 128 位的随机字节序列
            // this.uuid = CryptoJS.enc.Hex.stringify(uuid).toUpperCase();// 将字节序列转换为十六进制表示形式，并转换为大写字母
            this.allQaMsg.push({
                uuid: this.uuid,
                question: x.name,
                extendType: x.type,
                eventId: x.eventId,
                eventName: x.eventName,
                answer: '',
                callIng: true,
            })
            this.callIng = true;
        }
    }
});

